<?php
namespace app\admin\api;


use app\admin\model\token;
use think\Controller;
use \think\Session;
use app\admin\common\Http\ConferenceHttpClient;


//站点接口类
class Sites extends ConferenceHttpClient
{

    /**
     * 获取站点列表
     * @return [type] [description]
     */
    public function siteList()
    {
        //获取数据
        $url = '/v1/sites';

        $res['page'] = 1;
        $res['page_size'] = 1;

        //设置headers头
        $access_token = Session::get('user')->token->access_token;
        $headers['Authorization'] = $access_token;


        //请求接口信息
        $result = $this->get($url,$res,$headers);

        //用户信息存入session
        if ($result->code == 0){
            //Session::set('user',$result['obj']->data) ;
            return $result->data;

        }else{
            return $result->message;
        }
    }


    /**
     * 站点创建
     * @return [type] [description]
     */
    public function siteCreate($data)
    {
        if($data) {
            //获取数据
            $url = '/v1/sites';

            //设置headers头
            $access_token = Session::get('user')->token->access_token;
            $headers['Authorization'] =  $access_token;

            //请求接口信息
            $result = $this->post($url,$data,$headers);

            if ($result->code == 0 ){
                $app_id = $result->data->item->app->client->app_id;
                $app_secret = $result->data->item->app->client->app_secret;
//                $app_token = getapptoken($app_id,$app_secret);

                //存储站点token信息
                $token = new token();
//                $token->uid = $result->data->item->uid;
                $token->app_id = $app_id;
                $token->app_secret = $app_secret;
//                $token->auth_token = $app_token->auth_token;
//                $token->authorize_token = $app_token->authorize_token;
                $token->save();

                return 555;
            }else{
                return $result->message;
            }
        }
    }


    /**
     * 获取站点详情
     * @return [type] [description]
     */
    public function siteDetails($id)
    {
        //获取数据
        $url = '/v1/sites/'.$id;

        //设置headers头
        $access_token = Session::get('user')->token->access_token;
        $headers['Authorization'] = $access_token;

        $res = array();
        //请求接口信息
        $result = $this->get($url,$res,$headers);

        if ($result->code == 0){

            $app_id = $result->data->item->app->client->app_id;

            $to = new token();
            $id = $to->where("app_id=$app_id")->value('id');

            Session::set("appid[$id]",$app_id) ;

            return $result->data;
        }else{
            return $result->message;
        }
    }


    /**
     * 站点修改
     * @return [type] [description]
     */
    public function siteUpdate($id,$data)
    {
        if($data) {
            //获取数据
            $url = '/v1/sites/'.$id;

//            $dt['name'] = $data['name'];
//            $dt['domain'] = $data['domain'];
//            $dt['description'] = $data['description'];

            //设置headers头
            $access_token = Session::get('user')->token->access_token;
            $headers['Authorization'] = $access_token;


            //请求接口信息
            $result = $this->put($url,$data,$headers);

            if ($result->code == 0){
                return 555;
            }else{
                return $result->message;
            }
        }
    }


    /**
     * 删除站点
     * @return [type] [description]
     */
    public function siteDelete($id)
    {
        //获取数据
        $url = '/v1/sites/'.$id;

        //设置headers头
        $access_token = Session::get('user')->token->access_token;
        $headers['Authorization'] = $access_token;

        $res = array();
        //$res['id'] = $id;
        //请求接口信息
        $result = $this->delete($url,$res,$headers);

        if ($result->code == 0){
            return 555;
        }else{
            return $result->message;
        }
    }


}